package com.huadao.cart_ccs.base;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.common.adapter.CommonRecycleAdapter;

import java.util.Timer;
import java.util.TimerTask;

public abstract class BaseListFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    SwipeRefreshLayout swipeRefreshLayout;
    RecyclerView recycleView;

    protected CommonRecycleAdapter customeAdapter;
    private RecyclerView.ItemDecoration itemDecoration;
    private RecyclerView.LayoutManager layoutManager;
    private boolean enableLoadMore = true;
    private boolean enableRefresh = true;

    public abstract int getRecycleViewId();

    public abstract int getSwipeRefreshLayoutId();

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        recycleView=mRootView.findViewById(getRecycleViewId());
        swipeRefreshLayout=mRootView.findViewById(getSwipeRefreshLayoutId());
        init();
        customeAdapter = setAdapter();

        layoutManager = setLayoutManage();
        if(layoutManager==null){
            layoutManager= new LinearLayoutManager(this.getContext());
        }
        //设置布局管理器
        recycleView.setLayoutManager(setLayoutManage());
//        //设置为垂直布局，这也是默认的
//        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        //设置Adapter
        recycleView.setAdapter(customeAdapter);
        //设置分隔线
        itemDecoration=setItemDecoration();
        if(itemDecoration!=null) {
            recycleView.addItemDecoration(itemDecoration);
        }
        //设置增加或删除条目的动画
        recycleView.setItemAnimator(new DefaultItemAnimator());

        if(swipeRefreshLayout!=null) {
            swipeRefreshLayout.setOnRefreshListener(this);
            swipeRefreshLayout.setEnabled(enableRefresh);
        }

        loadData();
    }

    protected abstract void init();

    protected abstract RecyclerView.ItemDecoration setItemDecoration();

    protected abstract CommonRecycleAdapter setAdapter();

    public abstract RecyclerView.LayoutManager setLayoutManage();

    protected abstract void loadData();

    protected abstract void loadMoreData();

    public void setEnableLoadMore(boolean enableLoadMore) {
        this.enableLoadMore = enableLoadMore;
    }

    public void setEnableRefresh(boolean enableRefresh) {
        this.enableRefresh = enableRefresh;
    }

    protected void notifyDataSetChanged() {
        customeAdapter.notifyDataSetChanged();
    }

    protected void setSwipeRefresh(boolean isRefresh) {
        swipeRefreshLayout.setRefreshing(isRefresh);
    }

    @Override
    public void onRefresh() {
        if(swipeRefreshLayout==null){
            return;
        }
        Timer timer = new Timer();//实例化Timer类
        timer.schedule(new TimerTask() {
            public void run() {
                swipeRefreshLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        if(swipeRefreshLayout.isRefreshing()) {
                            showToast(getString(R.string.refresh_time_out));
                            swipeRefreshLayout.setRefreshing(false);
                        }
                    }
                });
                this.cancel();
            }
        }, 5000);//五百毫秒
        loadData();
    }

}
